import java.util.*;
import java.lang.*;

public class MayoPlayer3D extends Player {
	
	String color;
	int pieces;
	int player;
	int playerSpace;
	int otherPlayerSpace;
	int boardSize;

    public MayoPlayer3D(int _boardSize, int p)
    {
    	super(p, 'm');
    	pieces = 0;
    	boardSize = _boardSize;
		player = p;
		if(player == 0)
		{
			color = "green";
			playerSpace = -1;
			otherPlayerSpace = 1;
		}
		else
		{
			color = "blue";
			playerSpace = 1;
			otherPlayerSpace = -1;
		}	
    }
    
    public void makeMove3D(int boardSize, int[][][] boardState, int[] lastMove, Long maxTime, int[] newMove)
    {
    	System.out.println("--------------------------------");
    	System.out.println("Making a move" + color);
    	System.out.println("--------------------------------");

    	List<int[]> moves = getValidMoves(boardState, 0);
    	//System.out.println("CPU's Valid Moves:");
    	for (int i = 0; i < moves.size(); i++)
    	{
    		//System.out.println("x: " + moves.get(i)[0] + " y: " + moves.get(i)[1] + " z: " + moves.get(i)[2]);
    	}
    	Random generator = new Random();
		if(moves.size() == 0) //returning an invalid move will result in a pass
		{
			if (color == "green")
			{
				newMove[0] = -1;
				newMove[1] = 0;
				newMove[2] = 0;
			}
			else
			{
				newMove[0] = 0;
				newMove[1] = -1;
				newMove[2] = 0;
			}
		}
		else if (moves.size() > 1)
		{
			int randomIndex = generator.nextInt(moves.size()-1);
			int[] someMove;
			someMove = (int[])moves.get(randomIndex);
			newMove[0] = someMove[0];
			newMove[1] = someMove[1];
			newMove[2] = someMove[2];
			System.out.print(color+" making move x: " );
			System.out.print(newMove[0]);
			System.out.print(" y: ");
			System.out.println(newMove[1]);
			System.out.print(" z: ");
			System.out.println(newMove[2]);
		}
		else 
		{
			newMove[0] = ((int[])moves.get(0))[0];
			newMove[1] = ((int[])moves.get(0))[1];
			newMove[2] = ((int[])moves.get(0))[2];
		}
    }
    private boolean isValid(int[][][] boardState, int _x, int _y, int _z, int turn)
    {
    	int playerPiece;
    	int otherPlayerPiece;
    	if (turn == 0)
    	{
    		playerPiece = playerSpace;
    		otherPlayerPiece = otherPlayerSpace;
    	}
    	else
    	{
    		playerPiece = otherPlayerSpace;
    		otherPlayerPiece = playerPiece;
    	}
    	boolean valid = false;
		if(boardState [_x][_y][_z] == 0)
		{
			for(int i = -1; i <= 1; i++)
        	{
	        	for(int j = -1; j <= 1; j++)
	        	{
	        		for(int k = -1; k <= 1; k++)
	        		{
						boolean gotOne = false;
						
						//System.out.println("checking "+i+", "+j+", "+k); 
						
						int x = _x + i;
						int y = _y + j;
						int z = _z + k;
						
						while(x >= 0 && y >= 0 && z >= 0 && x < boardSize && y < boardSize && z < boardSize)
						{
							if(!gotOne && boardState[x][y][z] == otherPlayerPiece)
							{
								gotOne = true;
							}
							else if(gotOne && boardState[x][y][z] == 0)
							{
								break;
							}
							else if(gotOne && boardState[x][y][z] == playerPiece)
							{
								valid = true;
							}
							
							if (!gotOne)
								break;
							
							x += i;
							y += j;
							z += k;
						}
						//System.out.println(valid);
					}
				}
			}			
		}				
		return valid;
	}
    private List<int[]> getValidMoves(int[][][] boardState, int turn)
    {
    	List<int[]> validMoves = new ArrayList();
    	int[] aMove = new int[3];
    	for (int x = 0; x < boardSize; x++)
    	{
    		for (int y = 0; y < boardSize; y++)
    		{
    			for (int z = 0; z < boardSize; z++)
    			{
    				if (isValid(boardState, x, y, z, turn))
    				{
    					aMove[0] = x;
    					aMove[1] = y;
    					aMove[2] = z;
    					int[] a = {x, y, z}; 
    					validMoves.add(a);
    				}
    			}
    		}
    	}
    	return validMoves;
    }
}